//
//  TagsView.m
//  标签列表
//
//  Created by holier_zyq on 16/8/31.
//  Copyright © 2016年 holier_zyq. All rights reserved.
//

#import "TagsView.h"

@implementation TagsView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

- (void)addTagLabelWithArray:(NSArray *)tagArray{
    CGFloat tagsTotalWidth = 0;
//    CGFloat tagsWidth = 0;
    CGFloat tagsTotalHeigh = 0;
    CGFloat tagHeight = 0.0f;
    for (NSString *str in tagArray) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(tagsTotalWidth, tagsTotalHeigh, 0, 0)];
        label.font = [UIFont systemFontOfSize:12];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.cornerRadius = 2;
        label.layer.borderWidth = 0.5;
        label.layer.borderColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1].CGColor;
        label.text = str;
        label.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        [label sizeToFit];
        label.frame = CGRectMake(tagsTotalWidth, tagsTotalHeigh, CGRectGetWidth(label.frame) + 4, CGRectGetHeight(label.frame)+2);
        tagsTotalWidth += CGRectGetWidth(label.frame)+4;
        tagHeight = CGRectGetHeight(label.frame);
        
        if (tagsTotalWidth > self.frame.size.width) {
            tagsTotalHeigh += CGRectGetHeight(label.frame) + 4;
            tagsTotalWidth = 0;
            label.frame = CGRectMake(tagsTotalWidth, tagsTotalHeigh, CGRectGetWidth(label.frame) + 4, CGRectGetHeight(label.frame)+2);
            tagsTotalWidth += CGRectGetWidth(label.frame)+4;
            
        }
//        CGFloat a = CGRectGetHeight(label.frame);
//        NSLog(@"%f",a);
       
        [self addSubview:label];
        
    }
    tagsTotalHeigh += tagHeight;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, tagsTotalHeigh);
    
}

@end
